﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Collections.Specialized;
using System.Security.Cryptography.X509Certificates;
using System.IO;
using System.Xml;

namespace DataAccess
{
    public partial class DataAccess
    {
        public Entities.Storage_Account.StorageAccountProperty GetAccountProperty(String SubscriptionID, String CertificatePath, String ServiceName)
        {
            Entities.Storage_Account.StorageAccountProperty AccountProperty = new Entities.Storage_Account.StorageAccountProperty();

            string requestUrl = "https://management.core.windows.net/" + SubscriptionID + "/services/storageservices/" + ServiceName;
            string ReturnBody = string.Empty;


            WebResponse resp = null;
            try
            {
                NameValueCollection RequestHeaders = new NameValueCollection();
                RequestHeaders.Add("x-ms-version", x_ms_version);

                X509Certificate cert = X509Certificate.CreateFromCertFile(CertificatePath);
                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(requestUrl);
                request.Method = "GET";
                request.ClientCertificates.Add(cert);
                if (RequestHeaders != null)
                {
                    request.Headers.Add(RequestHeaders);
                }


                resp = request.GetResponse();
                //RequestID = resp.Headers["x-ms-request-id"];


                if (resp != null)
                {

                    using (StreamReader sr = new StreamReader(resp.GetResponseStream(), true))
                    {
                        ReturnBody = sr.ReadToEnd();
                        sr.Close();
                    }
                    if (ReturnBody != null)
                    {

                        XmlDocument xdoc = new XmlDocument();
                        xdoc.LoadXml(ReturnBody.ToString());
                        //XmlNodeList lst = xdoc.GetElementsByTagName("Url");
                        AccountProperty.Url = getXmlValue(xdoc, "Url");
                        //AccountProperty.ServiceName = getXmlValue(xdoc, "ServiceName");
                        //AccountProperty.AffinityGroup = getXmlValue(xdoc, "AffinityGroup");
                        AccountProperty.Description = getXmlValue(xdoc, "Description");
                        AccountProperty.Label = getXmlValue(xdoc, "Label");
                        //AccountProperty.Location = getXmlValue(xdoc, "Location");

                    }


                }
            }

            finally
            {

                if (resp != null) { resp.Close(); }
                resp = null;
            }

            return AccountProperty;
        }


    }
}
